	package main
	import (
		"fmt"
		"github.com/gin-gonic/gin"
		"net/http"
		"os"
		"path/filepath"
		"time"
	)
	const (
		UPLOAD_FOLDER = "/path/to/save" // 替换为你希望保存文件的路径
		HOST_IP       = "127.0.0.1"    // 从环境变量获取主机IP地址，或者直接指定
		PORT          = ":5008"
	)
	func main() {
		router := gin.Default()
		// 添加CORS中间件
		router.Use(corsMiddleware())
		// 确保上传文件夹存在
		os.MkdirAll(UPLOAD_FOLDER, os.ModePerm)
		router.POST("/upload/file", uploadFile)
		router.GET("/files/:filename", downloadFile)
		router.Run(PORT)
	}
	// CORS中间件
	func corsMiddleware() gin.HandlerFunc {
		return func(c *gin.Context) {
			c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
			c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
			c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
			if c.Request.Method == "OPTIONS" {
				c.AbortWithStatus(http.StatusNoContent)
				return
			}
			c.Next()
		}
	}
	func getTimestampFilename(originalFilename string) string {
		// 获取当前时间戳
		timestamp := time.Now().Format("20060102150405.000000")
		// 分离文件名和扩展名
		_, ext := filepath.Split(originalFilename)
		// 返回新的文件名
		return fmt.Sprintf("%s%s", timestamp, ext)
	}
	func uploadFile(c *gin.Context) {
		file, err := c.FormFile("file")
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": "No file part"})
			return
		}
		if file.Filename == "" {
			c.JSON(http.StatusBadRequest, gin.H{"error": "No selected file"})
			return
		}
		filename := getTimestampFilename(file.Filename)
		filePath := filepath.Join(UPLOAD_FOLDER, filename)
		if err := c.SaveUploadedFile(file, filePath); err != nil {
			c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save file"})
			return
		}
		downloadURL := fmt.Sprintf("http://%s%s/files/%s", HOST_IP, PORT, filename)
		c.JSON(http.StatusOK, gin.H{"message": "File uploaded successfully", "download_url": downloadURL})
	}
	func downloadFile(c *gin.Context) {
		filename := c.Param("filename")
		filePath := filepath.Join(UPLOAD_FOLDER, filename)
		if _, err := os.Stat(filePath); os.IsNotExist(err) {
			c.JSON(http.StatusNotFound, gin.H{"error": "File not found"})
			return
		}
		c.File(filePath)
	}
